<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Mail;//全局

class DataController extends Controller
{

    public function mail(){

        /*
        Mail::raw('邮件内容 测试',function($message){
            $message->from('705228812@qq.com','慕课网');
            $message->subject('邮件主题 测试');
            $message->to('438000095@qq.com');

        });
        */
        //发送html邮件
        Mail::send('student.mail',[
            'name'=>'小娥',
            'age'=>18,
            //'sutudent' => $student
        ],function($message){
            $message->from('705228812@qq.com','慕课网');
            $message->subject('邮件主题 测试12121');
            $message->to('438000095@qq.com');
        });
    }

    public function index()
    {
        //table
      //  $data = DB::table('pic')->get();
       // $data = DB::table('pic')->first();

       // $data=DB::table('user')->select("name","pass")->get();

       // $data=DB::table("user")->select(DB::raw("count(*) tot"))->get();
        //使用where条件
      //  $data= DB::table("user")->where("name","abc")->get();
        //$data= DB::table("user")->where("id",">",10)->get();
       // $data=DB::table("user")->where([['pass','123'],['id','<','5']])->get();
       // $data=DB::table("user")->where('pass','123')->orwhere('name','帅哥')->get();

        //排序
        //$data=\DB::table("user")->orderBy('id','desc')->get();
        //截取
        //offset 截取开始位置
        //limit 截取几个
        $data=DB::table("user")->offset('3')->limit('3')->get();
        //统计
        $data=DB::table("user")->count();
        $data=DB::table("user")->max('id');
        $data=DB::table("user")->min('id');
        $data=DB::table("user")->avg('id');
        $data=DB::table("user")->sum('id');
        //分组
        $data = DB::table('user')->select(DB::raw("pass,count(*) tot"))->groupBy("pass")->get();
        //聚合
        $data = DB::table('user')->select(DB::raw("pass,count(*) tot"))->having("tot",">",1)->groupBy("pass")->get();

        //多表查询
        //分类表 class id name
        //商品表 goods id gname cid
        //网站开发中使用多表查询
        //select class.name,goods.* from class,goods where class.id=goods.cid
      //  $data=DB::select("select class.name,goods.* from class,goods where class.id=goods.cid");
       //多表
        $data = DB::table("goods")->select("class.name","goods.*")->join("class",'class.id','=','goods.cid')->get();
        //左连接
        $data = DB::table("goods")->select("class.name","goods.*")->leftJoin("class",'class.id','=','goods.cid')->get();
       //右连接
        $data = DB::table("goods")->select("class.name","goods.*")->rightJoin("class",'class.id','=','goods.cid')->get();

        //增加
        $arr= array(
            "gname"=>"衣服",
            "cid"=>'5'
        );
        //$data=DB::table("goods")->insert($arr);

        $arr= array(
            array(
                "gname"=>"衣服1",
                "cid"=>'5'
            ),
            array(
                "gname"=>"衣服2",
                "cid"=>'5'
            ),

        );
        //$data=DB::table("goods")->insert($arr); //返回布尔值

        //获取增加id (ajax增加修改)
        //$data=DB::table("goods")->insertGetId(['gname'=>'农药001','cid'=>4]);

        //修改
       // $data= DB::table("goods")->where('id',8)->update(['gname'=>'美女']);
        //删除
        //返回影响行数
        //$data=DB::table("goods")->delete(8);
        //$data=DB::table("goods")->where('id',9)->delete();
        $data=DB::table("goods")->delete();
        dd($data);
    }

    //密码加密方法
     public function pass(){
      //echo md5('123');

      echo $pass=Crypt::encrypt('123');
      echo "<hr>";
      echo strlen($pass);
         echo "<hr>";
      echo Crypt::decrypt($pass);
    }

	
	
}
?>